<?php defined('_JEXEC') or die('Restricted access'); ?><?php defined('_SEXEC') or die('Restricted access'); ?>
<?php
if (!isset($_SESSION['username']) && !isset($_SESSION['usertype']))
    header("location: index.php");
?>
<?php
if (isset($submit)) {
    switch (trim($submit)) {
        case "Save":
            $booking_date = db_date_format($booking_date);
            foreach ($cn_no as $k => $v) {
                $sql = "insert into inventories(company_id,booking_date,cn_no,destination_id,document_id,weight,weight_unit,cour_charge,total_charge,status,datetime) values('" . $companies[$k] . "',\"$booking_date\",'" . $cn_no[$k] . "','" . $destinations[$k] . "','" . $documents[$k] . "','" . $weights[$k] . "','" . $weightunits[$k] . "','" . $courcharges[$k] . "','" . $totalcharges[$k] . "',1,now())";
                mysql_query($sql);
            }
            $msg = "Inventory added succesful";
            $booking_date = $series = $cn_no = $cn_no_to = "";
            break;
    }
}

$sql = "select id, companyname from companies where status = 1 order by companyname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $company_arr[$row['id']] = $row['companyname'];
}

$sql = "select * from destinations where status = 1 order by destination";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $destination_arr[$row['id']] = $row['destination'];
}
?>

<script>
    var cnexists= false;
    $(function() {
        $('#cn_no').blur(function() {
            $.post("getCN.php?cns=" + $("#series").val() + "&cnfrom=" + $("#cn_no").val(), function(data) {
                if (data == 'Success') {
                    cnexists= false;
                } else {
                    $('#error').html('<table id="error" align="center"><tbody><tr><td>&nbsp;&nbsp;Error(s) while processing your request</td></tr><tr><td><ol>' + data + '</ol></td></tr></tbody></table>');
                    cnexists=true;
                    $('#submitthis').addAttr('disabled');
                }
            });
        });
        
        $('#cn_no_to').blur(function() {
            $.post("getCN.php?cns=" + $("#series").val() + "&cnfrom=" + $("#cn_no").val()+'&cnto='+$("#cn_no_to").val(), function(data) {
                if (data == 'Success') {
                    cnexists= false;
                    $('#submitthis').removeAttr('disabled').focus();
                } else {
                    $('#error').html('<table id="error" align="center"><tbody><tr><td>&nbsp;&nbsp;Error(s) while processing your request</td></tr><tr><td><ol>' + data + '</ol></td></tr></tbody></table>');
                    cnexists=true;  
                    $('#submitthis').addAttr('disabled');
                    
                }
            });
        });
    })
    function GetCharge() {
        var cid = $("#company_id").val();
        var desid = $("#destination_id").val();
        var docid = $("#document_id").val();
        var slabfor = $("#slab_for").val();
        var wght = $("#weight").val();
        var wghtun = $("#weight_unit").val();
        if ((wght != '') && (!isNaN(wght))) {
            $.ajax({
                url: "charge.php",
                data: "company_id=" + cid + "&destination_id=" + desid + "&weight=" + wght + "&weight_unit=" + wghtun + "&document_id=" + docid + "&slab_for=" + slabfor,
                dataType: "json",
                success: function(data1) {
                    $('#cour_charge').val(data1.fright);
                    $('#total_charge').val(data1.total);
                }
            });
        } else {
            $('#cour_charge').val("");
            $('#total_charge').val("");
        }
    }


    function move_next() {
        var booking_date = $("#booking_date").val();
        var company_id = $("#company_id").val();
        var series = $("#series").val();
        var cn_no = $("#cn_no").val();
        var cn_no_to = $("#cn_no_to").val();

        if (booking_date == '') {
            alert("Enter Booking Date!!!");
            $("#booking_date").focus();
            return false;
        } else if (series == '') {
            alert("Enter C/N Series!!!");
            $("#series").focus();
            return false;
        } else if (cn_no == '') {
            alert("Enter Starting C/N No.");
            $("#cn_no").focus();
            return false;
        } else if (isNaN(cn_no)) {
            alert("Starting C/N No. should be Numeric!!!");
            $("#cn_no").focus();
            return false;
        } else if (cn_no_to == '') {
            alert("Enter Ending C/N No.");
            $("#cn_no_to").focus();
            return false;
        } else if (isNaN(cn_no_to)) {
            alert("Ending C/N No. should be Numeric!!!");
            $("#cn_no_to").focus();
            return false;
        }else if(cnexists){
            alert("Ending C/N No. already exists!!!");
            return false;
        }else{
            return true;
        }
    }


    function next_cn() {
        var series = $("#series").val();
        var cn_no = $("#cn_no").val();
        var cn_no_to = $("#cn_no_to").val();
        var cn = series + cn_no;
        var companyid = $("#company_id").val();
        var desid = $("#destination_id").val();
        var docid = $("#document_id").val();
        var wght = $("#weight").val();
        var wghtun = $("#weight_unit").val();
        var ccharge = $("#cour_charge").val();
        var tcharge = $("#total_charge").val();
        if ((wght != '') && (!isNaN(wght))) {

            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='cn_no[]' value='" + cn + "'>");
            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='companies[]' value='" + companyid + "'>");
            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='destinations[]' value='" + desid + "'>");
            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='documents[]' value='" + docid + "'>");
            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='weights[]' value='" + wght + "'>");
            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='weightunits[]' value='" + wghtun + "'>");
            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='courcharges[]' value='" + ccharge + "'>");
            document.getElementById("add_entry").insertAdjacentHTML('beforeEnd', "<input type='hidden' name='totalcharges[]' value='" + tcharge + "'>");
            cn_no = parseInt(cn_no) + 1;
            $("#cn_no").val(cn_no);
            if (parseInt(cn_no) <= parseInt(cn_no_to)) {
                document.getElementById("current_cn").innerHTML = series + cn_no;
            }

            if (parseInt(cn_no) >= parseInt(cn_no_to)) {
                $("#next").hide();
                $("#save").show("fast");
            }

        } else {
            alert("Enter Proper Weight!!!");
            $("#weight").focus();
        }
        $('#company_id').focus();

    }
</script>


<div id="title">Entry Form</div>

<?php
if (isset($next)) {
    $total_cn = $cn_no_to - $cn_no;
    if ($total_cn == 0) {
        $arr[] = "Starting C/N No. is equal to Ending C/N No.";
    } else if ($total_cn < 0) {
        $arr[] = "Starting C/N No. is greater than Ending C/N No.";
    } else {
        ?>
        <input type=hidden name=series id=series value=<?php echo $series ?> />
        <input type=hidden name=cn_no id=cn_no value=<?php echo $cn_no ?> />
        <input type=hidden name=cn_no_to id=cn_no_to value=<?php echo $cn_no_to ?> />

        <form name='entry-form2' action='' method='post' onsubmit='next_cn()'>
            <table border="0" cellspacing="3" cellpadding="3" align="center" >
                <tr>
                    <td colspan=2>
                        <?php
                        se();
                        ?>
                    </td>
                </tr>
                <tr>
                    <td align=right>Booking Date:</td>
                    <td>
                        <font color=red><?php echo $booking_date ?></font>
                        <input type="hidden" name="booking_date" value="<?php echo $booking_date ?>" />
                    </td>
                </tr>
                <tr>
                    <td align=right>C/N No.:</td>
                    <td>
                        <font color=red><span id="current_cn"><?php echo $series . $cn_no ?></span></font>
                    </td>
                </tr>
                <tr>
                    <td align=right>Company Name:</td>
                    <td>
                        <select name="company_id" id="company_id" onchange='GetCharge()'>
                            <?php
                            sopt($company_arr, $company_id)
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align=right>Destination:</td>
                    <td>
                        <select name="destination_id" id="destination_id" onchange='GetCharge()'>
                            <?php
                            sopt($destination_arr, $destination_id)
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align=right>Slab Type:</td>
                    <td>
                        <select name="document_id" id="document_id" onchange='GetCharge()'>
                            <?php
                            sopt($slab_type_arr, $document_id)
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align=right>Slab For:</td>
                    <td>
                        <select name="slab_for" id="slab_for" onchange='GetCharge()'>
                            <?php
                            sopt($slab_for_arr, $slab_for)
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align=right>Weight:</td>
                    <td>
                        <input name="weight" id="weight" type="text" onblur='GetCharge()'/>
                        <select name="weight_unit" id="weight_unit" onchange='GetCharge()'>
                            <?php
                            sopt($weight_unit_arr, $weight_unit)
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align=right>Courier Charge:</td>
                    <td><input name="cour_charge" id="cour_charge" type="text" /></td>
                </tr>
                <tr>
                    <td align=right>Total Charge:</td>
                    <td><input name="total_charge" id="total_charge" type="text" /></td>
                </tr>
                <tr>
                    <td colspan=2 align=center>
                        <a href="javascript:void(0)" onclick="next_cn()" id="next" class="button"> Next </a>
                        <input type="submit" name="submit" value="Save" class='button' id="save" style='display:none'/>
                    </td>
                </tr>
                <tr>
                    <td colspan=2>
                        <div id="add_entry"></div>
                    </td>
                </tr>
            </table>
        </form>
        <script>
            $( window ).load(function() {
               $('#company_id').focus(); 
            });
        </script>
        <?php
    }
}

if (!isset($next) || count($arr) > 0) {
    ?>
    <form name="entry-form" action="" method="post" onsubmit="javascript:return move_next()">
        <div id="error"></div>
        <table border="0" cellspacing="3" cellpadding="3" align="center">
            <tr>
                <td colspan=2>
                    <?php
                    se();
                    ?>
                </td>
            </tr>
            <tr>
                <td align=right>Booking Date:</td>
                <td>
                    <?php
                    $booking_date = ($booking_date == "") ? date('d-m-Y') : $booking_date;
                    ?>	
                    <input name="booking_date" id="booking_date" tabindex="1" type="text" value="<?php echo $booking_date ?>" onfocus="popcal(this)" />
                </td>
            </tr>
            <tr>
                <td align=right>C/N Series:</td>
                <td>
                    <input name="series" id="series" type="text" value="<?php echo $series ?>" tabindex="2" />
                </td>
            </tr>
            <tr>
                <td align=right>C/N Number From:</td>
                <td>
                    <input name="cn_no" id="cn_no" type="text" value="<?php echo $cn_no ?>" tabindex="3"  />
                </td>
            </tr>
            <tr>
                <td align=right>C/N Number To:</td>
                <td>
                    <input name="cn_no_to" id="cn_no_to" type="text" value="<?php echo $cn_no_to ?>" tabindex="4" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="next" value="Next" class='button' id="submitthis" disabled  tabindex="5"/>
                </td>
            </tr>	
        </table>
    </form>
    <?php
}
?>